/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.example;

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileWriter;
import java.io.InputStreamReader;
import java.io.Writer;
import java.net.URL;
import java.net.URLConnection;

/**
 *
 * @author Dilan
 * establish the connection with internet
 */
public class openURL {

    /**
     * open the connection with internet and send request get reply
     */
    public openURL() {
    }

    public void setProxy(String proxy, String port) {

        System.setProperty("http.proxyHost", proxy); //set proxy
        System.setProperty("http.proxyPort", port); //set port

    }

    public void URL(String word) {

        try {

            File file = new File("C:\\Temp\\write.txt");// open file to save web reply
            Writer output = new BufferedWriter(new FileWriter(file));

            URL url = new URL("http://www.google.com/dictionary?aq=f&langpair=en|en&q=" + word + "&hl=en"); //URL for send request
            URLConnection conn = url.openConnection();

            url.getPort(); //get port
            conn.setDoOutput(true);
            conn.setConnectTimeout(10000); //set connection timeout
            conn.setReadTimeout(10000);

            BufferedReader buffer = new BufferedReader(new InputStreamReader(conn.getInputStream())); // Get the response
            String line;
            /*write entier reply to the text file*/
            while ((line = buffer.readLine()) != null) {

                output.write(buffer.readLine());
            }
            buffer.close();
            output.close();
        } catch (Exception e) {
           
        }
    }
}
    
